package com.koushikdutta.async;

import com.koushikdutta.async.http.AsyncHttpClient;
import com.koushikdutta.async.http.AsyncHttpGet;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.http.AsyncHttpResponse;
import com.koushikdutta.async.util.LogUtil;
import com.koushikdutta.async.utils.FileUtils;
import ohos.utils.net.Uri;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;

public class ExampleOhosTest {
    private static final String LOGTAG = "AsyncSample_test-->";
    private Boolean testBl = false;

    @Test
    public void getHttpString() {
        testBl = false;
        Uri parse = Uri.parse("https://httpbin.org/get");
        AsyncHttpRequest get = new AsyncHttpRequest(parse, "GET");
        AsyncHttpClient.getDefaultInstance().executeString(get, new AsyncHttpClient.StringCallback() {
            @Override
            public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
                if(resultJudgment(e)){
                    return;
                }
                LogUtil.info(LOGTAG, result);
            }
        });

        delayOperation();
    }

    @Test
    public void getJsonUrl() {
        testBl = false;
        Uri parse = Uri.parse("http://api.androidhive.info/volley/person_object.json");
        AsyncHttpRequest get = new AsyncHttpRequest(parse, "GET");
        AsyncHttpClient.getDefaultInstance().executeJSONObject(get, new AsyncHttpClient.JSONObjectCallback() {
            @Override
            public void onCompleted(Exception e, AsyncHttpResponse source, ZSONObject result) {
                if(resultJudgment(e)){
                    return;
                }
                LogUtil.info(LOGTAG, result.toString());
            }
        });

        delayOperation();
    }

    @Test
    public void getJsonArray() {
        testBl = false;
        //此接口结果为字符串，会报错JSONObject cannot be converted to JSONArray,没有找到数组形式的测试url
        Uri parse = Uri.parse("https://wanandroid.com/wxarticle/chapters/json");
        AsyncHttpRequest get = new AsyncHttpRequest(parse, "GET");
        AsyncHttpClient.getDefaultInstance().executeJSONArray(get, new AsyncHttpClient.JSONArrayCallback() {
            @Override
            public void onCompleted(Exception e, AsyncHttpResponse source, ZSONArray result) {
                if(resultJudgment(e)){
                    return;
                }
                LogUtil.info(LOGTAG, result.toString());
            }
        });
    }

    @Test
    public void getImaFile() throws IOException {
        String imaUrl="https://github.com/dfaker/df/raw/master/finalmerged.jpg";
        AsyncHttpClient.getDefaultInstance().executeFile(new AsyncHttpGet(imaUrl), FileUtils.createFile(OhosAsync.getApp(), randomFile()), new AsyncHttpClient.FileCallback() {
            @Override
            public void onCompleted(Exception e, AsyncHttpResponse response, File result) {
                if(resultJudgment(e)){
                    return;
                }
              LogUtil.info(LOGTAG,"路径-->"+result.getPath());
            }
        });
    }


    /**
     * 请求结果信息判断
     */
    private boolean resultJudgment(Exception e) {
        if (e != null) {
            LogUtil.error(LOGTAG, e.getMessage());
            return true;
        }
        testBl = true;
        return false;

    }

    /**
     * 做个延迟 打印测试结果
     */
    private void delayOperation() {
        try {
            Thread.sleep(1000 * 5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Assert.assertTrue(testBl);
    }

    /**
     * 随机命名
     * @return string
     */
    private String randomFile() {
        SecureRandom random = new SecureRandom();

        int round = Math.round(random.nextInt(1001));
        return round + ".png";
    }
}